﻿using SuperJoe.Framwork.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WWX.Dto.Search;
using WWX.Admin.BLL;
using WWX.Admin.Web.Models;
using WWX.Model;
using SuperJoe.Framework.Core.Exceptions;

namespace WWX.Admin.Web.Controllers
{
    public class OrderController : AuthorizationController
    {
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult List(OrderAdminSearch search, int limit, int pageIndex = 0)
        {
            int results = 0;
            var list = new OrderBLL().GetPageList(search, ++pageIndex, limit, out results);
            return this.Json(new { results, rows = list.Select(l => new { id = l.Id, no = l.No, productInfo = l.ProductInfo, remark = l.Remark, date = l.Date.ToString(), logisticsInfo = l.LogisticsInfo, purchasePrice = l.PurchasePrice, salePrice = l.SalePrice, memberAccount = l.MemberAccount, memberTrueName = l.MemberTrueName, memberNickName = l.MemberNickName, memberPhone = l.MemberPhone, status = l.Status, statusText = l.Status.ToText(), isDeducted = l.IsDeducted, remainPaymentMoney = l.RemainPaymentMoney }) }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Save(OrderVM vm)
        {
            var bll = new OrderBLL();
            var order = vm.ToOrderSaveDto();
            if (order.Id.IsNullOrWhiteSpace())
            {
                bll.Add(order);
            }
            else
            {
                bll.Update(order);
            }
            return this.CreateRichJsonResult();
        }

        public JsonResult UpdateStatus(string id, OrderStatus? status)
        {
            if (status == null)
            {
                ThrowHelper.ThrowFriendlyException("请输入状态");
            }

            new OrderBLL().UpdateStatus(id, status.Value);
            return this.CreateRichJsonResult();
        }

        public JsonResult DeductMoney(string id)
        {
            new OrderBLL().DeductMoney(id);
            return this.CreateRichJsonResult();
        }
    }
}